package top.lixunda.ecommerce.server.good.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.controller.IGoodGroupController;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodGroupDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.GoodGroupInsertDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.GoodGroupUpdateDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.GoodGroupTreeVO;
import top.lixunda.ecommerce.server.good.api.entity.vo.GoodGroupVO;
import top.lixunda.ecommerce.server.good.service.IGoodGroupService;

import java.util.List;

/**
 * @author Xumda
 * @version time:2020/2/6 22:39
 */
@Slf4j
@RestController
public class GoodGroupController extends BaseController
        implements IGoodGroupController {

    private final IGoodGroupService goodGroupService;

    @Autowired
    public GoodGroupController(IGoodGroupService goodGroupService) {
        this.goodGroupService = goodGroupService;
    }

    /**
     * 添加一个商品分组
     *
     * @param goodGroupInsertDTO 新添加的商品分组数据
     * @return 是否添加成功
     */
    @Override
    public MessageObject<Boolean> insertGoodGroup(GoodGroupInsertDTO goodGroupInsertDTO) {
        boolean success = goodGroupService.insertGoodGroup(goodGroupInsertDTO);
        return success ? getSuccessMessageObject("添加商品分组数据成功", true) :
                getErrorMessageObject("添加商品分组数据失败", false);
    }

    /**
     * 更新一个商品分组
     *
     * @param goodGroupUpdateDTO 需要更新的商品分组数据
     * @return 是否更新成功
     */
    @Override
    public MessageObject<Boolean> updateGoodGroup(GoodGroupUpdateDTO goodGroupUpdateDTO) {
        boolean success = goodGroupService.updateGoodGroup(goodGroupUpdateDTO);
        return success ? getSuccessMessageObject("更新商品分组数据成功", true) :
                getErrorMessageObject("更新商品分组数据失败", false);
    }

    /**
     * 根据id删除商品分组，只删除该节点，其子节点全部放到父节点中去
     *
     * @param id 商品分组id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeGoodGroupById(Integer id) {
        boolean success = goodGroupService.removeGoodGroupById(id);
        return success ? getSuccessMessageObject("移除商品分组数据成功", true) :
                getErrorMessageObject("移除商品分组数据失败", false);
    }

    /**
     * 根据id删除商品分组，只删除该节点，其子节点全部放到父节点中去
     *
     * @param ids 商品分组id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeGoodGroupByIds(List<Integer> ids) {
        boolean success = goodGroupService.removeGoodGroupByIds(ids);
        return success ? getSuccessMessageObject("移除商品分组数据成功", true) :
                getErrorMessageObject("移除商品分组数据失败", false);
    }

    /**
     * 根据id删除商品分组，删除包括子节点的所有节点
     *
     * @param id 商品分组id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeGoodGroupAllById(Integer id) {
        boolean success = goodGroupService.removeGoodGroupAllById(id);
        return success ? getSuccessMessageObject("移除商品分组数据成功", true) :
                getErrorMessageObject("移除商品分组数据失败", false);
    }

    /**
     * 根据id删除商品分组，删除包括子节点的所有节点
     *
     * @param ids 商品分组id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeGoodGroupAllByIds(List<Integer> ids) {
        boolean success = goodGroupService.removeGoodGroupAllByIds(ids);
        return success ? getSuccessMessageObject("移除商品分组数据成功", true) :
                getErrorMessageObject("移除商品分组数据失败", false);
    }

    /**
     * 根据id获取分组数据信息
     *
     * @param id 分组id
     * @return 单条分组数据
     */
    @Override
    public MessageObject<GoodGroupVO> queryById(Integer id) {
        GoodGroupDO goodGroupDO = goodGroupService.queryById(id);
        GoodGroupVO vo = GoodGroupVO.fromGoodGroupDO(goodGroupDO);
        return getSuccessMessageObject("获取数据成功", vo);
    }

    /**
     * 查询整个分组树
     *
     * @return 分组树
     */
    @Override
    public MessageObject<GoodGroupTreeVO> queryTree() {
        GoodGroupTreeVO treeVO = goodGroupService.queryTree();
        return getSuccessMessageObject("获取数据成功", treeVO);
    }

    /**
     * 根据父节点id获取子类节点列表
     *
     * @param id 父节点id
     * @return 子节点列表
     */
    @Override
    public MessageObject<List<GoodGroupVO>> queryChildrenByParentId(Integer id) {
        List<GoodGroupDO> list = goodGroupService.queryChildrenByParentId(id);
        List<GoodGroupVO> resultList = GoodGroupVO.fromGoodGroupDO(list);
        return getSuccessMessageObject("获取数据成功", resultList);
    }

    /**
     * 查询首层节点
     *
     * @return 首层节点
     */
    @Override
    public MessageObject<List<GoodGroupVO>> queryRoots() {
        List<GoodGroupDO> list = goodGroupService.queryRoots();
        List<GoodGroupVO> resultList = GoodGroupVO.fromGoodGroupDO(list);
        return getSuccessMessageObject("获取数据成功", resultList);
    }

}
